package com.ruoyi.qbms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qbms.domain.QbmsSubject;
import com.ruoyi.qbms.service.IQbmsSubjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qbms.domain.QbmsCategory;
import com.ruoyi.qbms.service.IQbmsCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 题库类别Controller
 * 
 * @author zouhuu
 * @date 2022-10-16
 */
@RestController
@RequestMapping("/qbms/qbmsCategory")
public class QbmsCategoryController extends BaseController
{
    @Autowired
    private IQbmsCategoryService qbmsCategoryService;

    @Autowired
    private IQbmsSubjectService qbmsSubjectService;

    /**
     * 查询树结构选项
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(QbmsCategory qbmsCategory) {
        return AjaxResult.success(qbmsCategoryService.selectQbmsCategoryOptions(qbmsCategory));
    }

    /**
     * 查询题库类别列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsCategory:list')")
    @GetMapping("/list")
    public AjaxResult list(QbmsCategory qbmsCategory)
    {
        List<QbmsCategory> list = qbmsCategoryService.selectQbmsCategoryList(qbmsCategory);
        return AjaxResult.success(list);
    }

    /**
     * 导出题库类别列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsCategory:export')")
    @Log(title = "题库类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QbmsCategory qbmsCategory)
    {
        List<QbmsCategory> list = qbmsCategoryService.selectQbmsCategoryList(qbmsCategory);
        ExcelUtil<QbmsCategory> util = new ExcelUtil<QbmsCategory>(QbmsCategory.class);
        util.exportExcel(response, list, "题库类别数据");
    }

    /**
     * 获取题库类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qbmsCategoryService.selectQbmsCategoryById(id));
    }

    /**
     * 新增题库类别
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsCategory:add')")
    @Log(title = "题库类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbmsCategory qbmsCategory)
    {
        return toAjax(qbmsCategoryService.insertQbmsCategory(qbmsCategory));
    }

    /**
     * 修改题库类别
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsCategory:edit')")
    @Log(title = "题库类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbmsCategory qbmsCategory)
    {
        return toAjax(qbmsCategoryService.updateQbmsCategory(qbmsCategory));
    }

    /**
     * 删除题库类别
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsCategory:remove')")
    @Log(title = "题库类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            QbmsCategory qbmsCategory = new QbmsCategory();
            qbmsCategory.setParentId(id);
            List<QbmsCategory> qbmsCategories = qbmsCategoryService.selectQbmsCategoryList(qbmsCategory);
            if (qbmsCategories.size() > 0) {
                return AjaxResult.error("该分类下存在子分类，不能删除");
            }
            QbmsSubject qbmsSubject = new QbmsSubject();
            qbmsSubject.setCategoryId(id);
            List<QbmsSubject> qbmsSubjects = qbmsSubjectService.selectQbmsSubjectOptions(qbmsSubject);
            if (qbmsSubjects.size() > 0) {
                return AjaxResult.error("该分类下存在科目，不能删除");

            }
        }
        return toAjax(qbmsCategoryService.deleteQbmsCategoryByIds(ids));
    }
}
